package api

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
	"web_end/forms"
	"web_end/global/response"
	"web_end/proto"
	"web_end/utils"
)

func HandleProjectResponse(raw *proto.ProjectResponseInfo) *response.ProjectResponse {
	return &response.ProjectResponse{
		ProjectId:    raw.Id,
		ProjectName:  raw.Name,
		ProjectDesc:  raw.Desc,
		ProjectCover: raw.Cover,
	}
}

// CreateProject /* 创建项目
func CreateProject(ctx *gin.Context) {
	var createProjectForm forms.CreateProjectForm
	if err := ctx.ShouldBindJSON(&createProjectForm); err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeBadRequest,
			"msg":  response.MsgParamsError,
		})
		return
	}

	userServerClient := *utils.CreateUserServerGrpc("GetUserById", "创建项目")
	// 查询用户是否存在
	_, err := userServerClient.GetUserById(ctx, &proto.IdRequest{
		UserId: createProjectForm.LeaderId,
	})
	if err != nil {
		zap.S().Errorw("[GetUserById] 查询 [用户] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	// 用户是否具有权限创建项目 - project-server进行判断
	projectServerClient := *utils.CreateProjectGrpc("CreateProject", "创建项目")
	rsp, err := projectServerClient.CreateProject(ctx, &proto.CreateProjectInfo{
		Name:     createProjectForm.Name,
		Desc:     createProjectForm.Desc,
		Cover:    createProjectForm.Cover,
		GroupId:  createProjectForm.GroupId,
		LeaderId: createProjectForm.LeaderId,
	})
	if err != nil {
		zap.S().Errorw("[CreateProject] 创建 [项目] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
		"data": HandleProjectResponse(rsp),
	})
}

// GetProjectById /* 通过项目ID查询项目
func GetProjectById(ctx *gin.Context) {
	projectIdStr := ctx.Param("id")
	projectIdInt, _ := strconv.Atoi(projectIdStr)

	projectServerClient := *utils.CreateProjectGrpc("GetProjectById", "查询项目")
	rsp, err := projectServerClient.GetProjectById(ctx, &proto.ProjectIdInfo{
		Id: uint64(projectIdInt),
	})
	if err != nil {
		zap.S().Errorw("[GetProjectById] 查询 [项目] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	data := HandleProjectResponse(rsp)
	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
		"data": data,
	})
}

// GetProjectListByGroupId /* 通过团队ID查询项目列表
func GetProjectListByGroupId(ctx *gin.Context) {
	groupIdStr := ctx.Param("id")
	groupIdInt, _ := strconv.Atoi(groupIdStr)

	projectServerClient := *utils.CreateProjectGrpc("GetProjectListByGroupId", "查询项目列表")
	rsp, err := projectServerClient.GetProjectListByGroupId(ctx, &proto.GroupIdInfo{
		Id: uint64(groupIdInt),
	})
	if err != nil {
		zap.S().Errorw("[GetProjectListByGroupId] 查询 [项目列表] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	var projectList []response.ProjectResponse
	for _, project := range rsp.ProjectList {
		projectList = append(projectList, *HandleProjectResponse(project))
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
		"data": projectList,
	})
}

// UpdateProject /* 更新项目
func UpdateProject(ctx *gin.Context) {
	var updateProjectForm forms.UpdateProjectForm
	if err := ctx.ShouldBindJSON(&updateProjectForm); err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeBadRequest,
			"msg":  response.MsgParamsError,
		})
		return
	}

	projectServerClient := *utils.CreateProjectGrpc("UpdateProject", "更新项目")
	_, err := projectServerClient.UpdateProject(ctx, &proto.UpdateProjectInfo{
		Id:      updateProjectForm.ProjectId,
		Name:    updateProjectForm.ProjectName,
		Desc:    updateProjectForm.ProjectDesc,
		Cover:   updateProjectForm.ProjectCover,
		UserId:  updateProjectForm.UserId,
		GroupId: updateProjectForm.GroupId,
	})
	if err != nil {
		zap.S().Errorw("[UpdateProject] 更新 [项目] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
	})
}

// DeleteProject /* 删除项目
func DeleteProject(ctx *gin.Context) {
	var deleteProjectForm forms.DeleteProjectForm
	if err := ctx.ShouldBindJSON(&deleteProjectForm); err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeBadRequest,
			"msg":  response.MsgParamsError,
		})
		return
	}

	projectServerClient := *utils.CreateProjectGrpc("DeleteProject", "删除项目")
	_, err := projectServerClient.DeleteProject(ctx, &proto.DeleteProjectInfo{
		Id:      deleteProjectForm.ProjectId,
		GroupId: deleteProjectForm.GroupId,
		UserId:  deleteProjectForm.UserId,
	})
	if err != nil {
		zap.S().Errorw("[DeleteProject] 删除 [项目] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
	})
}
